package enemy.states;

import player.Player;
import enemy.Enemy;
import game.Level;

public class Sighting extends State {

	private Enemy enemy;
	private Player player;


	public Sighting(Enemy enemy) {
		this.enemy = enemy;
	}

	@Override
	public void doAction(Level level) {

		//if you can't see them go back to patrol mode

		if(level.canSight(enemy) == null) {
			enemy.setPatrol();
		}
		
		//otherwise
		
		//continue charging sightedness
		
		float visibility = player.visibility();

		float pX = player.getX();
		float pY = player.getY();

		float eX = enemy.getX();
		float eY = enemy.getY();

		float diffX = Math.abs(pX - eX);
		float diffY = Math.abs(pY - eY);

		enemy.setDist((float) Math.hypot(diffX, diffY));

		float dist = (enemy.distanceToPlayer()/Enemy.MAX_VISION); //actual distance/total possible distance larger better

		dist = 1 - dist; //now smaller is better

		//lower dist and visibility is better
		float ave = (dist+visibility)/2;

		float maxTicks = (float) (1 / (240*Math.exp(-2.773*ave)));

		enemy.setSightedness(enemy.getSightedness() + maxTicks);
		enemy.setAve(maxTicks);
		
		//continue facing player
		enemy.turnToFace(player.getX(), player.getY());
		
		if(enemy.getSightedness() >= 1f) {
			float rotation = enemy.getTargetRotation(player.getX(), player.getY());
			enemy.setRotation(rotation);
			enemy.setShooting(player);
		}

	}

	public void setPlayer(Player p) {
		this.player = p;

	}


}

